#include "SocketAddress.hpp"

#include <arpa/inet.h>

#include <sstream>

namespace se {

SocketAddress::SocketAddress(const char *ip, unsigned short port) {
    this->_address.sin_family = AF_INET;
    this->_address.sin_addr.s_addr = ::inet_addr(ip);
    this->_address.sin_port = ::htons(port);
}

SocketAddress::SocketAddress(const struct sockaddr_in &address) : _address(address) { }

std::string SocketAddress::getIp() const {
    return ::inet_ntoa(this->_address.sin_addr);
}

unsigned short SocketAddress::getPort() const {
    return this->_address.sin_port;
}

struct sockaddr_in *SocketAddress::getAddress() {
    return &this->_address;
}

std::string SocketAddress::toString() const {
    std::ostringstream oss;
    oss << this->getIp() << ":" << this->getPort();
    return oss.str();
}

}  // namespace se